package com.cskaoyan.controller.wx_address;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MemoryBean;
import com.cskaoyan.bean.bo.wx_address.AddressSaveBO;
import com.cskaoyan.bean.vo.wx_address.AddressDetailVO;
import com.cskaoyan.bean.vo.wx_address.AddressListVO;
import com.cskaoyan.service.wx_address.WxAddressService;
import com.cskaoyan.util.common.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: market
 * @description: 地址方法分发
 * @author: zhong
 * @create: 2022-07-21 20:59
 **/
@RestController
@RequestMapping("wx/address")
public class WxAddressController {
    @Autowired
    WxAddressService wxAddressService;
    @Autowired
    MemoryBean memoryBean;

    private Integer getUserId(@RequestHeader(Constant.WX_TOKEN_HEADER) String token) {
        Integer userId = memoryBean.tokenAndUserId.get(token);
        if (userId == null) {
            return -1;
        }
        return userId;
    }

    /**
     * @param "userId":memoryBean中获取
     * @return 地址列表
     * @auther zhong
     * @description wx/address/list 地址列表
     */
    @GetMapping("list")
    public BaseRespVo list(@RequestHeader(Constant.WX_TOKEN_HEADER) String token) {
        Integer userId = getUserId(token);
        if (userId == -1) {
            return BaseRespVo.ok(null, "登录信息失效", Constant.NOT_FOUND_CODE);
        }
        AddressListVO addressList = wxAddressService.listAddressOp(userId);
        return BaseRespVo.ok(addressList);
    }

    /**
     * @param id:address的id，"userId":memoryBean中获取
     * @return 地址详细信息
     * @auther zhong
     * @description wx/address/detail 显示地址详细信息
     */
    @GetMapping("detail")
    public BaseRespVo detail(Integer id, @RequestHeader(Constant.WX_TOKEN_HEADER) String token) {
        Integer userId = getUserId(token);
        if (userId == -1) {
            return BaseRespVo.ok(null, "登录信息失效", Constant.NOT_FOUND_CODE);
        }
        AddressDetailVO detailVO = wxAddressService.getDetailOp(id);
        return BaseRespVo.ok(detailVO);
    }

    /**
     * @param "userId":memoryBean中获取
     * @return addressId
     * @auther zhong
     * @description wx/address/save 新增或修改地址信息
     */
    @PostMapping("save")
    public BaseRespVo save(@RequestBody AddressSaveBO addressInfo, @RequestHeader(Constant.WX_TOKEN_HEADER) String token) {
        Integer userId = getUserId(token);
        if (userId == -1) {
            return BaseRespVo.ok(null, "登录信息失效", Constant.NOT_FOUND_CODE);
        }
        Integer addressId;
        if (addressInfo.getId() == 0) {
            //id为0，新增
            addressId = wxAddressService.addAddressOp(addressInfo, userId);
        } else {
            //id不为0，修改
            addressId = wxAddressService.updateAddressOp(addressInfo, userId);
        }
        return BaseRespVo.ok(addressId);
    }

    /**
     * @param "userId":memoryBean中获取
     * @return 删除成功或失败
     * @auther zhong
     * @description wx/address/delete 删除地址信息
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody int id, @RequestHeader(Constant.WX_TOKEN_HEADER) String token) {
        Integer userId = getUserId(token);
        if (userId == -1) {
            return BaseRespVo.ok(null, "登录信息失效", Constant.NOT_FOUND_CODE);
        }
        int code = wxAddressService.delAddressOp(id);
        if (code == Constant.NOT_FOUND_CODE) {
            return BaseRespVo.invalidParameter("地址信息不存在");
        }
        return BaseRespVo.ok();
    }
}
